package com.ysm.smfu.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.ysm.smfu.R;
import com.ysm.smfu.activity.My_XiuGai_Activity;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.view.TimeButton;
import com.ysm.smfu.view.XEditText;
import com.ysm.smfu.welcome.SharedConfig;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/9/28.
 */
public class LoginPsdSetFragment extends MyBaseFragment {


    @BindView(R.id.et_activity_register_phonenumber)
    XEditText etActivityRegisterPhonenumber;
    @BindView(R.id.et_activity_register_yanzhengma)
    EditText etActivityRegisterYanzhengma;
    @BindView(R.id.btn_activity_register_getyanzhengma)
    TimeButton btnActivityRegisterGetyanzhengma;
    private String num;
    private String phoneNum;
    private String name;
    private String code2;
    private String loginPsd;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = UIUtils.inflate(R.layout.fragment_psd_login);
        ButterKnife.bind(this, view);
        //设置分隔属性
        etActivityRegisterPhonenumber.setPhone();
        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        name = sp.getString(SpKey.NAME, "");
        Bundle arguments = getArguments();
        if (arguments != null)
//        登录页找回密码过来的标志
            loginPsd = arguments.getString("loginPsd");
        return view;
    }

    @OnClick({R.id.btn_activity_register_getyanzhengma, R.id.fragment_zhifu_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_activity_register_getyanzhengma:
                num = etActivityRegisterPhonenumber.getText().toString().trim();
                phoneNum = num.replace(" ", "");
                if (!phoneNum.matches(SpKey.NUMREGEX)) {
                    showErrorDialog("电话号码格式不正确");
                    return;
                }
//                如果不是登录页的找回密码，判断是否和登录密码一致
                if (TextUtils.isEmpty(loginPsd))
                    if (!TextUtils.equals(phoneNum, name)) {
                        showErrorDialog("手机号码必须和登录号码一致");
                        return;
                    }
                if (!btnActivityRegisterGetyanzhengma.isTime()) {
                    btnActivityRegisterGetyanzhengma.startTime();
                }
                /**
                 * 获取验证码
                 */
                getLoginYzCode();
                break;
            case R.id.fragment_zhifu_next:
                num = etActivityRegisterPhonenumber.getText().toString().trim();
                phoneNum = num.replace(" ", "");
                if (!phoneNum.matches(SpKey.NUMREGEX)) {
                    showErrorDialog("电话号码格式不正确");
                    return;
                }
                code2 = etActivityRegisterYanzhengma.getText().toString().trim();
                if (TextUtils.isEmpty(code2)) {
                    showErrorDialog("请先获取验证码");
                    return;
                }
                chechYzCode(code2);
                break;
        }
    }

    /**
     * 错误对话框
     */
    private void showErrorDialog(String msg) {
        AlertDialog dialog = new AlertDialog.Builder(getActivity()).setMessage(msg)
                .setNegativeButton("确定", null)
                .create();
        dialog.show();
    }


    /**
     * 获取验证码
     */
    private void getLoginYzCode() {
        BaseProtocal.builder(true).getLoginYzCode(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                PrintUtil.printRespones("修改登录密码" , response);
                String state = JsonUtil.getState(response);
                if (!TextUtils.equals(state, "1")) {
                    // 如果用户以注册，则停止读秒
                    btnActivityRegisterGetyanzhengma.endTime();
                    ToastUtils.showToast(JsonUtil.getMessage(response));
                    return;
                }
                etActivityRegisterYanzhengma.requestFocus();
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.showErrToast();
            }
        }, phoneNum, loginPsd);
    }

    /**
     * 检测验证码
     *
     * @param code
     * @return
     */
    public boolean chechYzCode(String code) {
        BaseProtocal.builder(true).checkYzCode(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                PrintUtil.printRespones("检测验证码" ,response);
                String state = JsonUtil.getState(response);
                if (!TextUtils.equals(state, "1")) {
                    ToastUtils.showToast(JsonUtil.getMessage(response));
                    return;
                }
                jumpToNext();
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.showToast(getActivity().getResources().getString(R.string.netError));
            }
        }, phoneNum, code, loginPsd);
        return true;
    }

    /**
     * 跳转下一页面
     */
    public void jumpToNext() {
        Intent intent = new Intent(getActivity(), My_XiuGai_Activity.class);
        intent.putExtra("position", 6);
        intent.putExtra(SpKey.CODE, code2);
        intent.putExtra(SpKey.PHONE, phoneNum);
        intent.putExtra("loginPsd", loginPsd);
        getActivity().startActivity(intent);
        getActivity().finish();
    }
}
